package com.zhouzi.zhaoyang.util;

import android.app.Dialog;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.tencent.wxpayface.IWxPayfaceCallback;
import com.tencent.wxpayface.WxPayFace;
import com.zhouzi.zhaoyang.bean.AuthCodeBean;
import com.zhouzi.zhaoyang.bean.OrderDetailBean;
import com.zhouzi.zhaoyang.mvp.p.PayActivityPresenter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class MyHttp {


    private static String TAG = "MyHttp";
    private TimePickerView pvTime;

    public static void report(OrderDetailBean orderDetail) {
        Map<String, Object> m1 = new HashMap<String, Object>();
        m1.put("mch_id", orderDetail.getMchId()); // 商户号
        m1.put("sub_mch_id", orderDetail.getSubMchId()); // 子商户号(非服务商模式不填)
        m1.put("out_trade_no", orderDetail.getOutTradeNo());  // 填写商户订单号
        WxPayFace.getInstance().reportOrder(m1, new IWxPayfaceCallback() {
            @Override
            public void response(Map info) throws RemoteException {
                if (info == null) {
                    new RuntimeException("调用返回为空").printStackTrace();
                    return;
                }
                String code = (String) info.get("return_code");
                String msg = (String) info.get("return_msg");
                Log.d(TAG, "response info :: " + code + " | " + msg);
                if (code == null || !code.equals("SUCCESS")) {
                    new RuntimeException("调用返回非成功信息: " + msg).printStackTrace();
                    return;
                }
                Log.e(TAG, "调用返回成功");

            }
        });
    }

    public static void getUserInfo(AuthCodeBean data, Map map, PayActivityPresenter p) {

        Map<String, String> m1 = new HashMap<String, String>();
        m1.put("appid", data.getAppid()); // 公众号，必填
        m1.put("mch_id", data.getMchId()); // 商户号，必填
//        m1.put("sub_appid", data.getSubAppId()); // 子商户公众账号ID(非服务商模式不填)
        m1.put("sub_mch_id", data.getSubMchId()); // 子商户号(非服务商模式不填)
        m1.put("store_id", data.getStoreId()); // 门店编号，必填
        m1.put("face_authtype", "FACEID-ONCE"); // 人脸识别模式， FACEID-ONCE`: 人脸识别(单次模式) FACEID-LOOP`: 人脸识别(循环模式), 必填
        m1.put("authinfo", data.getAuthinfo()); // 调用凭证，详见上方的接口参数
        m1.put("auth_mode", "1"); // 未授权用户是否跳转授权页面    0：不跳转    1：跳转

        Log.e("会员人脸支付：", "调用");

        WxPayFace.getInstance().getWxpayfaceUserInfo(m1, new IWxPayfaceCallback() {
            @Override
            public void response(Map info) throws RemoteException {

                Log.e("sub_openid:", info.toString() + ":" + info.get("return_msg"));


                if (info == null) {
                    new RuntimeException("调用返回为空").printStackTrace();
                    return;
                }
                String code = (String) info.get("return_code"); // 错误码
                String msg = (String) info.get("return_msg"); // 错误码描述
                String openid = info.get("openid").toString(); // openid
                String sub_openid = "";
                if (info.get("sub_openid") != null)
                    sub_openid = info.get("sub_openid").toString(); // 子商户号下的openid(服务商模式)


                map.put("subOpenId", sub_openid);
                p.memberPay(map, data);


                String nickName = info.get("nickname").toString(); // 微信昵称
                if (code == null || openid == null || nickName == null || !code.equals("SUCCESS")) {
                    new RuntimeException("调用返回非成功信息,return_msg:" + msg + "   ").printStackTrace();
                    return;
                }


            }
        });


    }

    public void initTimePicker(Context context) {

        pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = getTime(date);
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setType(new boolean[]{true, true, true, true, true, true})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(5) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }

        pvTime.show();

    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }


}
